﻿Imports ajProgressBar = ajayumi.develop.VBasic.Win.Controls.ProgressBar
Imports ajayumi.develop.win32.WebResourcesCatcher.Service
Imports ajayumi.develop.VBasic.Win.Controls

Public Class TpDgvResource

#Region "Variables"

    Private m_WebResourceManager As WebResourceManager = Nothing

#End Region

#Region "Properties"

    Private m_ResTaskInfo As ResTaskInfo
    Public Property ResTaskInfo() As ResTaskInfo
        Get
            Return m_ResTaskInfo
        End Get
        Set(ByVal value As ResTaskInfo)
            m_ResTaskInfo = value
            Me.m_WebResourceManager.Domain = value.Domail
            Me.DgvImages1.ResTaskInfo = value
            Me.WebClient1.HtmlEncode = value.HtmlEncode
        End Set
    End Property


#End Region

    Public Sub New()

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        Me.m_WebResourceManager = New WebResourceManager()
        AddHandler Me.m_WebResourceManager.AnaylzeCompleted, Sub(sender As Object, e As AnalyzeCompletedEventArgs)
                                                                 Me.setText(Me.ProgressBar1, String.Format("{0} 页面的资源提取完毕", e.Url))
                                                                 Me.setText(Me.lbTaskMsg, String.Format("待处理任务数：{0}", Me.m_WebResourceManager.GetTaskQueueCount()))
                                                                 Me.DgvImages1.LoadAsync(e.ResList)

                                                                 If Not e.Cancelled Then
                                                                     Dim _Url As String = Me.m_WebResourceManager.Next()
                                                                     Me.WebClient1.GetHtmlCodeAsync(_Url)
                                                                 End If
                                                             End Sub

    End Sub

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()

        '########################################################################
        '取资源文件中的图片
        Dim _Assembly As System.Reflection.Assembly = Nothing
        Dim _ResourceManager As System.Resources.ResourceManager = Nothing

        Try
            _Assembly = System.Reflection.Assembly.Load("ajayumi.develop.win32.WebResourcesCatcher.Res.Default")
            _ResourceManager = New System.Resources.ResourceManager("ajayumi.develop.win32.WebResourcesCatcher.Res.Default.default", _Assembly)


            Me.tsbtnContinue.Image = DirectCast(_ResourceManager.GetObject("_play"), Image)
            Me.tsbtnPause.Image = DirectCast(_ResourceManager.GetObject("_pause"), Image)
            Me.tsbtnStop.Image = DirectCast(_ResourceManager.GetObject("_stop"), Image)

            Me.tsbtnClearLvItem.Image = DirectCast(_ResourceManager.GetObject("_clearLv"), Image)
            Me.tsbtnRemoveSelected.Image = DirectCast(_ResourceManager.GetObject("_removeselected"), Image)
            Me.tsbtnRemoveDownloaded.Image = DirectCast(_ResourceManager.GetObject("_removedownloaded"), Image)

            Me.tsbtnSelectAll.Image = DirectCast(_ResourceManager.GetObject("_selectall"), Image)
            Me.tsbtnUnSelectAll.Image = DirectCast(_ResourceManager.GetObject("_unselectall"), Image)

            Me.tsbtnDownload.Image = DirectCast(_ResourceManager.GetObject("_download"), Image)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            _Assembly = Nothing
            _ResourceManager = Nothing
        End Try

        '########################################################################
    End Sub

    ''' <summary>
    ''' 设置进度条文本值
    ''' </summary>
    ''' <param name="control"></param>
    ''' <param name="text"></param>
    ''' <remarks></remarks>
    Private Sub setText(ByVal control As Label, ByVal text As String)
        If Me.ProgressBar1.InvokeRequired Then
            Me.ProgressBar1.Invoke(New Action(Of Label, String)(AddressOf Me.setText), New Object() {control, text})
        Else
            control.Text = text
        End If
    End Sub

    ''' <summary>
    ''' 设置进度条文本值
    ''' </summary>
    ''' <param name="control"></param>
    ''' <param name="text"></param>
    ''' <remarks></remarks>
    Private Sub setText(ByVal control As ajProgressBar, ByVal text As String)
        If Me.ProgressBar1.InvokeRequired Then
            Me.ProgressBar1.Invoke(New Action(Of ajProgressBar, String)(AddressOf Me.setText), New Object() {control, text})
        Else
            control.CustomText = text
        End If
    End Sub

    ''' <summary>
    ''' 设置进度条进度值
    ''' </summary>
    ''' <param name="control"></param>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Private Sub setValue(ByVal control As ajProgressBar, ByVal value As Integer)
        If Me.ProgressBar1.InvokeRequired Then
            Me.ProgressBar1.Invoke(New Action(Of ajProgressBar, Integer)(AddressOf Me.setValue), New Object() {control, value})
        Else
            control.Value = value
        End If
    End Sub

    ''' <summary>
    ''' 工具条按钮状态
    ''' </summary>
    ''' <param name="control"></param>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Private Sub setEnabled(ByVal control As ToolStripButton, ByVal value As Boolean)
        If Me.ToolStrip1.InvokeRequired Then
            Me.ToolStrip1.Invoke(New Action(Of ToolStripButton, Boolean)(AddressOf Me.setEnabled), New Object() {control, value})
        Else
            control.Enabled = value
        End If
    End Sub

    ''' <summary>
    ''' 设置 继续, 暂停, 停止 按钮是否启用
    ''' </summary>
    ''' <param name="tsbtnContinueEnabled"></param>
    ''' <param name="tsbtnPauseEnable"></param>
    ''' <param name="tsbtnStopEnable"></param>
    ''' <remarks></remarks>
    Private Sub setButtonStatus(ByVal tsbtnContinueEnabled As Boolean, ByVal tsbtnPauseEnable As Boolean, ByVal tsbtnStopEnable As Boolean)
        Me.setEnabled(Me.tsbtnContinue, tsbtnContinueEnabled)
        Me.setEnabled(Me.tsbtnPause, tsbtnPauseEnable)
        Me.setEnabled(Me.tsbtnStop, tsbtnStopEnable)
    End Sub


    ''' <summary>
    ''' 开始
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Start()
        Me.WebClient1.GetHtmlCodeAsync(Me.ResTaskInfo.SiteUrl)
        Me.setButtonStatus(False, True, True)
    End Sub

    ''' <summary>
    ''' 关闭
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Close()
        Me.m_WebResourceManager.Save()
    End Sub

    Private Sub tsbtnContinue_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnContinue.Click
        Me.m_WebResourceManager.Continue()
        Me.setButtonStatus(False, True, True)
    End Sub

    Private Sub tsbtnPause_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnPause.Click
        Me.m_WebResourceManager.Cancel()
        Me.setButtonStatus(True, False, True)
    End Sub

    Private Sub tsbtnStop_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnStop.Click
        Me.m_WebResourceManager.Cancel()
        Me.setButtonStatus(False, False, False)
    End Sub

    Private Sub tsbtnClearLvItem_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnClearLvItem.Click
        Me.DgvImages1.ClearAll()
    End Sub

    Private Sub tsbtnRemoveSelected_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnRemoveSelected.Click
        Me.DgvImages1.RemoveCheckedItems()
    End Sub

    Private Sub tsbtnRemoveDownloaded_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnRemoveDownloaded.Click
        Me.DgvImages1.RemoveDownloadedItems()
    End Sub

    Private Sub tsbtnSelectAll_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnSelectAll.Click
        Me.DgvImages1.CheckAll()
    End Sub

    Private Sub tsbtnUnSelectAll_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnUnSelectAll.Click
        Me.DgvImages1.UnCheckAll()
    End Sub

    Private Sub tsbtnDownload_Click(sender As System.Object, e As System.EventArgs) Handles tsbtnDownload.Click
        Me.DgvImages1.DownloadCheckedItems()
    End Sub

    Private Sub WebClient1_DownloadProgressChanged(sender As System.Object, e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
        Me.setValue(Me.ProgressBar1, e.ProgressPercentage)
    End Sub

    Private Sub WebClien1_GetHtmlCodeCompleted(sender As System.Object, e As GetHtmlCodeCompletedEventArgs) Handles WebClient1.GetHtmlCodeCompleted
        If e.Error IsNot Nothing Then
            Console.WriteLine(e.Error.Message)
            If e.Error.InnerException IsNot Nothing Then
                Console.WriteLine(e.Error.InnerException.Message)
            End If
        End If

        Me.m_WebResourceManager.Analyze(e.SiteUrl, e.HtmlCode)
    End Sub


End Class
